import { Dialog } from 'vant'
//必须带done回调 执行完成调用
// import { ElMessage } from 'element-plus'
import trim from 'lodash/trim'
export default function pdaShowModal({ vm, action, done, callbackData }) {
  console.log('pdaShowModal', vm, action.extension)
  const title = action.extension.title.value
  const message = action.extension.message.value
  const btnControls = action.extension.btnControls.value

  const useLastData = action.extension.useLastData.value
  // 对获取到的消息内容做变量拆分 -- #{}符号包裹内容视为变量
  let messageContent = ''
  if (useLastData) {
    // 如果使用上次任务数据，则通过key获取消息内容
    if (message.indexOf('->') !== -1) {
      const fieldList = message.split('->')
      messageContent = callbackData
      fieldList.forEach((fieldItem) => {
        // 如果没有后续值则不继续往下取值
        if (messageContent[trim(fieldItem)] === undefined) return
        messageContent = messageContent[trim(fieldItem)]
      })
    } else {
      //
      const staticList = message.split(/#{.*?}/gi)
      const variableList = message.match(/#{(.+?)\}/g)
      // console.log('位置', index * 2 + 1, index, staticList)
      if (variableList && variableList.length) {
        // 如果有配置变量 则先获取变量值
        variableList.forEach((variableItem, index) => {
          let variableContent = ''
          variableItem = trim(variableItem.slice(2, -1))
          if (variableItem.indexOf('->') !== -1) {
            const fieldList = variableItem.split('->')
            variableContent = callbackData
            fieldList.forEach((fieldItem) => {
              // 如果没有后续值则不继续往下取值
              if (variableContent[trim(fieldItem)] === undefined) return
              variableContent = variableContent[trim(fieldItem)]
            })
          } else {
            variableContent = callbackData[variableItem]
          }

          staticList.splice(index * 2 + 1, 0, variableContent)
        })
        messageContent = staticList.join('')
      } else {
        callbackData[message] ? (messageContent = callbackData[message]) : (messageContent = message)
      }
    }
  } else {
    messageContent = message
  }
  console.log('messageContent', messageContent)

  Dialog.confirm({
    title: title,
    message: messageContent,
    showConfirmButton: true,
    showCancelButton: btnControls,
  })
    .then(() => {
      console.log('确认')
      done(null, {
        status: 'true',
      })
    })
    .catch(() => {
      console.log('取消')
      done(null, {
        status: 'false',
      })
    })
  // done(null, {})
}
